﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace Com.Binaria.SISCOB.Web.Paginas
{
    public partial class Error : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            if (Server.GetLastError() == null) return;
            Exception ex = Server.GetLastError().GetBaseException();
            //
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<b>Message</b>: <b>" + ex.Message.ToString() + "</b>");
            sb.AppendLine("<b>Exception</b>: " + ex.GetType().ToString() + "");
            if (ex.TargetSite != null)
                sb.AppendLine("<b>Targetsite</b>: " + ex.TargetSite.ToString() + "");
            sb.AppendLine("<b>Source</b>: " + ex.Source + "");
            sb.AppendLine("<b>StackTrace</b>: " + ex.StackTrace.ToString().Replace(Environment.NewLine, "") + "");
            sb.AppendLine("<b>Data count</b>: " + ex.Data.Count.ToString() + "");
            if (ex.Data.Count > 0)
            {
                HtmlTable tbl = new HtmlTable();
                tbl.Border = 1;
                HtmlTableRow htr = new HtmlTableRow();
                HtmlTableCell htc1 = new HtmlTableCell();
                HtmlTableCell htc2 = new HtmlTableCell();
                HtmlTableCell htc3 = new HtmlTableCell();
                HtmlTableCell htc4 = new HtmlTableCell();
                htc1.InnerHtml = "<b>Key</b>";
                htc2.InnerHtml = "<b>Value</b>";
                htc3.InnerHtml = "Key Type";
                htc4.InnerHtml = "Value Type";

                htr.Cells.Add(htc1);
                htr.Cells.Add(htc2);
                htr.Cells.Add(htc3);
                htr.Cells.Add(htc4);
                tbl.Rows.Add(htr);

                foreach (DictionaryEntry de in ex.Data)
                {
                    HtmlTableRow tr = new HtmlTableRow();
                    HtmlTableCell tc1 = new HtmlTableCell();
                    HtmlTableCell tc2 = new HtmlTableCell();
                    HtmlTableCell tc3 = new HtmlTableCell();
                    HtmlTableCell tc4 = new HtmlTableCell();
                    tc1.InnerHtml = "<b>" + de.Key + "</b>";
                    tc2.InnerHtml = "<b>" + de.Value + "</b>";
                    tc3.InnerHtml = de.Key.GetType().Name;
                    tc4.InnerHtml = de.Value.GetType().Name;
                    tc3.Align = "center";
                    tc4.Align = "center";

                    tr.Cells.Add(tc1);
                    tr.Cells.Add(tc2);
                    tr.Cells.Add(tc3);
                    tr.Cells.Add(tc4);
                    tbl.Rows.Add(tr);
                }
                StringBuilder tblSb = new StringBuilder();
                System.IO.StringWriter sw = new System.IO.StringWriter(tblSb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                tbl.RenderControl(htw);
                sb.AppendLine(tblSb.ToString());
            }
            sb.AppendLine("");
            sb.AppendLine("<b>Exception</b>: " + ex.ToString().Replace(Environment.NewLine, "") + "");
            Server.ClearError();
            //
            Response.Write(sb.ToString());

        }
    }
}
